<?php

namespace App\Admin\Controllers;

use App\Admin\Metrics\Users\AuthUsers;
use App\Admin\Repositories\WxUser;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Metrics\Users\TotalUsers;
use App\Admin\Metrics\Users\NewUsers;
use App\Admin\Metrics\Users\VipUsers;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;

class WxUserController extends AdminController
{

    public function index(Content $content)
    {
        return $content
            ->header('用户')
            ->description('微信小程序用户')
            ->body(function (Row $row) {
                $row->column(4,new TotalUsers());
                $row->column(4,new AuthUsers());
                $row->column(4,new VipUsers());
            })
            ->body($this->grid());
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxUser(), function (Grid $grid) {
            $grid->model()->orderBy('id','desc');
            $grid->showColumnSelector();
//            $grid->column('id')->sortable();
            $grid->column('user_name');
            $grid->column('user_avatar')->image('',50);
            $grid->column('user_background_maps')->image('',50);
//            $grid->column('user_background_maps');
//            $grid->column('user_introduce');
//            $grid->column('user_birthday');
//            $grid->column('user_mobile');
//            $grid->column('weixin_name')->explode()->label();
//            $grid->column('weixin_openid');
            $grid->column('a','其他')->display('更多内容')->modal('详情',function ($res){
                $user = $this;

                return view('admin/user/info',compact('user'));
            });
//            $grid->column('country');
//            $grid->column('province');
//            $grid->column('city');
//            $grid->column('others')->display(function () {
//                return '认证:'. $this->is_authentication .'<br/>'.
//                       '会员' . $this->is_member.'<br/>'.
//                       '官方' . $this->is_official ? '是':'否';
//            });
            $grid->column('gender')->using([0 => '未知',1 => '男',2 => '女'])->label();
//            $grid->column('authentication_id');
            $grid->column('is_authentication')->switch();
            $grid->column('is_member')->switch();
            $grid->column('is_official')->switch();
            $grid->column('user_state')->using([0=>'正常',1=>'用户注销',2=>'管理员封号'])->label();
            $grid->column('updated_at','登录时间')->sortable();


            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('user_name');
                $filter->equal('gender')->select([0 => '未知',1 => '男',2 => '女']);
                $filter->equal('is_member')->select([0=>'否',1=>'是']);
                $filter->equal('is_official')->select([0=>'否',1=>'是']);
            });
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxUser(), function (Show $show) {
            $show->field('id');
            $show->field('user_name');
            $show->field('user_avatar');
            $show->field('user_background_maps')->image();
            $show->field('user_introduce');
            $show->field('user_birthday');
            $show->field('user_mobile');
            $show->field('wiexin_name');
            $show->field('wiexin_openid');
            $show->field('country');
            $show->field('province');
            $show->field('city');
            $show->field('authentication_id');
            $show->field('is_authentication');
            $show->field('is_member');
            $show->field('is_official');
            $show->field('user_state');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxUser(), function (Form $form) {
            $form->display('id');
            $form->text('user_name');
            $form->image('user_avatar');
            $form->image('user_background_maps');
            $form->radio('is_authentication')->options([0=>'否',1=>'是']);
            $form->radio('is_member')->options([0=>'否',1=>'是']);
            $form->radio('is_official')->options([0=>'否',1=>'是']);
            $form->radio('user_state')->options([0=>'正常',1=>'用户注销',2=>'管理员封号']);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
